package cn.passlink.www.ServerImpl;
import cn.passlink.www.Enity.Article;
import cn.passlink.www.Enity.sysUser;
import cn.passlink.www.Mapper.ArticleDao;
import cn.passlink.www.Mapper.sysUserDao;
import cn.passlink.www.Server.sysUserServer;
import cn.passlink.www.Utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class sysUserServerImpl implements sysUserServer {

    @Autowired
    private sysUserDao UserDao;

    @Autowired
    private ArticleDao ArticleDao;

    /**
     * getContact
     * 获取用户简介
     *
     * @param article_id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/10/14
     **/

    @Override
    public Map<String, Object> getContact(String article_id) {


        try {
            //  找用户id
            String user_id = UserDao.findUserByArticleId(article_id);
//找用户
            sysUser user = UserDao.findById(user_id);
            if (user == null) {
                return JsonUtils.resultJson(403, "用户不存在");
            }
            Map<String, Object> res = new HashMap<>();
            res.put("avatar", user.getAvatar());
            res.put("email", "隐私不展示，谢谢");
            res.put("career_tags", user.getCareer_tags());
            res.put("nickname", user.getNickname());
            res.put("introduction", user.getIntroduction());
            res.put("background_image", user.getBackground_image());
            res.put("lock_status", user.getLock_status());

            List<Article> list = ArticleDao.findAllByUserId(user.getUser_id());
            int articleCounts = 0;
            int questionCounts = 0;
            for (Article item : list) {
                if (item.getType().equals("文章")) {
                    articleCounts = articleCounts + 1;
                }
                if (item.getType().equals("问题")) {
                    questionCounts = questionCounts + 1;
                }
            }
            res.put("articleCounts", articleCounts);
            res.put("questionCounts", questionCounts);
            return JsonUtils.dataJson(200, res, "获取用户信息成功");

        } catch (Exception e) {
            log.error("获取失败", e);
            return JsonUtils.resultJson(500, "获取失败");
        }

    }

    @Override
    public Map<String, Object> getDetials(String user_id) {
        try {
            sysUser user = UserDao.findById(user_id);
            if (user == null) {
                log.error("用户不存在");
                return JsonUtils.resultJson(404, "获取用户信息失败！！！");
            }
            user.setPassword(null);
            user.setCreate_time(null);
            return JsonUtils.dataJson(200, user, "获取用户信息成功");
        } catch (Exception e) {
            log.error("获取用户信息失败", e);
            return JsonUtils.resultJson(500, "获取用户信息失败");
        }


    }

    @Override
    public Map<String, Object> updateDetials(String user_id, String avatar, String career_tags, String nickname, String introduction, String background_image) {
        sysUser user = new sysUser();
        user.setUser_id(user_id);
        user.setAvatar(avatar);
        user.setCareer_tags(career_tags);
        user.setNickname(nickname);
        user.setIntroduction(introduction);
        user.setBackground_image(background_image);
        try {
            UserDao.updateById(user);
            return JsonUtils.resultJson(200, "更新成功！！！");
        } catch (Exception e) {
            log.error("更新失败", e);
            return JsonUtils.resultJson(500, "更新失败");
        }


    }

    @Override
    public Map<String, Object> getUserCounts() {
        try {
            int counts = UserDao.getCounts();
            return JsonUtils.dataJson(200, counts, "");
        } catch (Exception e) {
            log.error("获取失败", e);
            return JsonUtils.resultJson(500, "");
        }

    }
}
